# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libavif
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.2
pkgrel=3
pkgdesc="Library for encoding and decoding .avif files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/AOMediaCodec/libavif"
license=('BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-dav1d"
         $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-i686* || \
               ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || \
            echo "${MINGW_PACKAGE_PREFIX}-rav1e" )
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
             "${MINGW_PACKAGE_PREFIX}-nasm"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/AOMediaCodec/libavif/archive/v${pkgver}.tar.gz"
        002-pixbuf-install-repfix.patch)
sha512sums=('04400ae76214d2f0361a14897d6ee97be675375865bb96c8d237e9a4a1152ac1a966db903c11df82da71b0bc68599a5857e038cc90d63c5d3bc77b13169a3e75'
            'f0fd57f8b5dff73cbdb0805ce3bbe74116489898fe1332007c08fb83a770c14ef0e18b311afd8908e0097c7bc17a99b4f4ed029912c619a3f3e334c5449e8b3a')

prepare() {
  cd ${_realname}-${pkgver}

  # https://gitlab.kitware.com/cmake/cmake/-/issues/19632
  patch -p1 -i ${srcdir}/002-pixbuf-install-repfix.patch
}

build() {
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p build-${CARCH} && cd build-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G Ninja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DAVIF_BUILD_APPS=ON \
    -DAVIF_CODEC_AOM=ON \
    -DAVIF_CODEC_DAV1D=ON \
    -DAVIF_CODEC_RAV1E=$( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-i686* || \
                             ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] &&
                          echo "OFF" || echo "ON" ) \
    -DAVIF_BUILD_GDK_PIXBUF=ON \
    -DAVIF_BUILD_TESTS=ON \
    -DAVIF_ENABLE_WERROR=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

check() {
  cd ${srcdir}/build-${CARCH}
  ${MINGW_PREFIX}/bin/ctest ./ || true
}

package() {
  cd ${srcdir}/build-${CARCH}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/README.md ${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README.md
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
